<template>
  <div>
    <el-form ref="loginForm" :rules="rules" :model="loginForm" class="loginContainer">
      <h3 class="loginTitle">系统登录</h3>
      <el-form-item prop="username" label="账号">
        <el-input type="text" size="small" v-model="loginForm.username" placeholder="请输出账号" style="width: 80%">
        </el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input type="password" size="small" v-model="loginForm.password" placeholder="请输出密码" style="width: 80%">
        </el-input>
      </el-form-item>
      <el-form-item prop="code" label="验证码">
        <el-input type="text" size="small" auto-complete="false" v-model="loginForm.code" style="width: 45%"
                  placeholder="点击图片更换验证码"></el-input>
        <el-image size="small" :src="captchaUrl" @click="updateCaptchaUrl" class="icon"></el-image>
        <div>
          <el-checkbox :checked="checked" class="loginRemember">记住我</el-checkbox>
        </div>
        <el-button type="primary" @click="submitForm('ruleForm')" style="width: 100%">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {postRequest} from '../utils/api';

export default {
  name: "Login",
  data() {
    return {
      captchaUrl: '/captcha?time=' + new Date(),
      loginForm: {
        username: "admin",
        password: "123",
        code: "",
      },
      checked: true,
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          postRequest("/login", this.loginForm).then(resp => {
            if (resp) {
              this.loading = true;
              // 存储用户token
              const tokenStr = resp.object.tokenHead + resp.object.token;
              //存进session中
              window.sessionStorage.setItem("tokenStr", tokenStr);
              // this.$router.replace("/home")
              // 获得上一个跳转路由的参数，参数名redirect
              let path = this.$route.query.redirect
              //若是跳转来登录页（也就是此页）或者没输入路径，则跳转到主页，若是其他路径，则直接放行
              this.$router.replace((path == '/' || path == undefined) ? '/home' : path)
            }
            if (resp == undefined) {
              this.loading = false;
            }
          })
        } else {
          this.message({
            message: '请输入所有字段！',
            type: 'error'
          });
          return false;
        }
      });
    },
    updateCaptchaUrl() {
      this.captchaUrl = '/captcha?time=' + new Date()
    }

  }
}
</script>

<style scoped>
.loginContainer {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 15px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c5;
}

.loginTitle {
  margin: 0px auto 40px auto;
  text-align: center;
}

.loginRemember {
  text-align: left;
  margin: 0px 0px 15px 0px;
}

.icon {
  vertical-align: middle;
  margin-left: 8px;
  height: 32px;
}

</style>